import json
import os
from datetime import datetime


def is_date_format(string):
    """
    检查字符串是否符合 YYYYMMDD 格式。
    """
    try:
        datetime.strptime(string, '%Y%m%d')
        return True
    except ValueError:
        return False


def process_directory(directory_path, directory_date, all_json_data):
    """
    处理目录中的所有 .txt 文件，并解析 JSON 内容。
    """
    try:
        files_in_directory = os.listdir(directory_path)
    except FileNotFoundError:
        print(f"The directory {directory_path} does not exist.")
        return

    # 筛选出所有txt文件
    txt_files = [file for file in files_in_directory if file.endswith('.txt')]

    # 初始化一个空的字典来存储当前目录的解析后的JSON数据
    json_data = {}

    # 处理每个txt文件
    for file in txt_files:
        try:
            # 读取文件内容
            file_path = os.path.join(directory_path, file)
            with open(file_path, 'r', encoding='utf-8') as file_obj:
                content = file_obj.read()

            # 解析JSON内容
            parsed_data = json.loads(content)

            # 遍历 diff 列表，为每条数据添加 dt 字段
            diff_data = parsed_data.get('data', {}).get('diff', [])
            for item in diff_data:
                item['dt'] = directory_date  # 使用目录名称作为日期

            # 在最外层添加 trandt 字段
            parsed_data['trandt'] = directory_date

            json_data[file] = parsed_data

        except json.JSONDecodeError:
            # 如果文件内容不是有效的JSON，则跳过该文件
            print(f"File {file} in directory {directory_path} is not a valid JSON. Skipping.")
            continue
        except Exception as e:
            # 处理其他可能的异常
            print(f"An error occurred while processing file {file} in directory {directory_path}: {e}")
            continue

    # 将当前目录的解析后的数据添加到总数据中
    if json_data:
        all_json_data[directory_path] = json_data

def process_directory_data(directory_path, directory_date, all_json_data):
    """
    处理目录中的所有 .txt 文件，并解析 JSON 内容。
    """
    try:
        files_in_directory = os.listdir(directory_path)
    except FileNotFoundError:
        print(f"The directory {directory_path} does not exist.")
        return

    # 筛选出所有txt文件
    txt_files = [file for file in files_in_directory if file.endswith('.txt')]

    # 初始化一个空的字典来存储当前目录的解析后的JSON数据
    json_data = {}

    # 处理每个txt文件
    for file in txt_files:
        try:
            # 读取文件内容
            file_path = os.path.join(directory_path, file)
            with open(file_path, 'r', encoding='utf-8') as file_obj:
                content = file_obj.read()

            # 解析JSON内容
            parsed_data = json.loads(content)

            # 遍历 diff 列表，为每条数据添加 dt 字段
            diff_data = parsed_data.get('data', {}).get('diff', [])
            for item in diff_data:
                item['dt'] = directory_date  # 使用目录名称作为日期

            # 在最外层添加 trandt 字段
            parsed_data['trandt'] = directory_date

            json_data = parsed_data

        except json.JSONDecodeError:
            # 如果文件内容不是有效的JSON，则跳过该文件
            print(f"File {file} in directory {directory_path} is not a valid JSON. Skipping.")
            continue
        except Exception as e:
            # 处理其他可能的异常
            print(f"An error occurred while processing file {file} in directory {directory_path}: {e}")
            continue

    # 将当前目录的解析后的数据添加到总数据中
    if json_data:
        all_json_data = json_data
def main():
    # 假设我们想要读取的根目录路径
    root_directory = 'D:\\PythonWorks\\pythonlearning\\01test\\tiantianjijin'  # 请确保这个路径存在

    # 初始化一个空的字典来存储所有解析后的JSON数据
    all_json_data = {}

    # 列出根目录中的所有文件和子目录
    try:
        subdirectories = [d for d in os.listdir(root_directory) if os.path.isdir(os.path.join(root_directory, d))]
    except FileNotFoundError:
        print(f"The directory {root_directory} does not exist.")
        return

    # 处理符合日期格式的子目录
    for subdir in subdirectories:
        if is_date_format(subdir):
            subdir_path = os.path.join(root_directory, subdir)
            process_directory(subdir_path, subdir, all_json_data)
            # process_directory_data(subdir_path, subdir, all_json_data)

    # 输出结果
    for directory_name, data in all_json_data.items():
        print(f"\nProcessed data from directory {directory_name}:")
        # print(json.dumps(data, indent=4, ensure_ascii=False))
        print(data)

    # 如果需要将结果保存到新的文件中，可以取消注释以下代码：
    with open(os.path.join(root_directory, 'all_processed_data.json'), 'w', encoding='utf-8') as outfile:
        json.dump(all_json_data, outfile, indent=4, ensure_ascii=False)


if __name__ == "__main__":
    main()
